<?php

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
		'DebugKit.Toolbar',
        'Auth' => array(
        	'loginAction' => array('admin' => false, 'controller' => 'login', 'action' => 'index'),
            'loginRedirect' => array('admin' => false, 'controller' => 'login', 'action' => 'index'),
        	'logoutRedirect' => array('admin' => false, 'controller' => 'pages','action' => 'display'),
        	'authError' => 'You must be logged in to view this page.',
        	'loginError' => 'Invalid Username or Password entered, please try again.'
        ),
		'Session'
	);
	
	public $helpers = array('Html', 'Form', 'Session');
	
	public $theme = 'Default';
	public function beforeFilter() {
		$this->theme = 'Default';
		if ($this->Auth->loggedIn()) {
			$user = $this->Session->read('Auth.User');
			$this->set('user', $user);
		}
		//allow all action in a ccontroller
		//$this->Auth->allow();
	}
	
	public function isAuthorized($user) {
	    if (empty($this->params['admin'])) {
	        return true;
	    } else if(AuthComponent::user('role') == 'admin'){
	        return true;
	    }
	    return false;
	}
	public function getTopic(){
		$conditions = array("Topic.deleted" => 0);
		$this->loadModel('Topic');
		$data = $this->Topic->find('all',array('conditions' => $conditions));
		$this->set("menuTopic",$data);
		$this->set("userId",CakeSession::read("Auth.User.id"));
		$this->set("userName",CakeSession::read("Auth.User.username"));
	}
}
